using System.Collections.Generic;
using System.Data.Objects;
using System.Web.Mvc;
using AtomicCms.Common.IDataContext;
using AtomicCms.Common.Models;
using AtomicCms.Data.EntityFramework;

namespace AtomicCms.Tests.Controllers
{
    using Common.Entities;
    using Common.Logging;
    using Data.Abstract;
    using Moq;
    using NHibernate;
    using NUnit.Framework;
    using Services.Abstract;
    using Services.Concrete;
    using Web.Controllers;
    using Web.Core.Mvc;

    [TestFixture]
    public class HomeControllerBehavior
    {
        private Mock<IAtomicCmsDataContext> _dataContext;
        #region Setup/Teardown

        [SetUp]
        public void Setup()
        {
            this.pageData = new Mock<IPageData>();
            var settingData = new Mock<ISettingData>();
            var log = new Mock<ILoggingService>();
            var userData = new Mock<IUserData>();
            var configurationManager = new Mock<IConfigurationManager>();


            ISettingService settingService = new SettingService(settingData.Object, configurationManager.Object);
            _dataContext = new Mock<IAtomicCmsDataContext>();
            /*_dataContext.SetupGet(x => x.ac_Pages).Returns(new Mock<IObjectSet<ac_Pages>>().Object);
            _dataContext.SetupGet(x => x.ac_Tags).Returns(new Mock<IObjectSet<ac_Tags>>().Object);
            _dataContext.SetupGet(x => x.ac_PageTags).Returns(new Mock<IObjectSet<ac_PageTags>>().Object);
            */
            IAtomicCmsDataRepository dataRepository = new AtomicCmsDataRepository(null) ;
            IPageService pageService = new PageService(this.pageData.Object, settingData.Object, log.Object,
                                                       userData.Object, dataRepository);

            this.c = new HomeController(pageService, settingService, log.Object);
        }

        #endregion

        private Mock<IPageData> pageData;

        private HomeController c;

        [Test]
        public void Should_Permanent_redirect_if_wrong_alias_provided_for_page_id()
        {
            int pageId = 3;
            CmsPage expectedPage = new CmsPage(){Id = pageId, Alias = "expected-alias"};
            pageData.Setup(x => x.Load(pageId)).Returns(expectedPage);
            LegacyUrl301Result result = this.c.Content(pageId, "this-is-wrong_alias") as LegacyUrl301Result;
            Assert.IsNotNull(result);
            string expectedUrl = expectedPage.GetUrl();
            Assert.AreEqual(expectedUrl, result.NewUrl);
        }

        [Test]
        public void Should_Throw_Exception_If_Page_Not_Found()
        {
            const int pageId = 999;

            this.pageData.Setup(x => x.Load(pageId)).Throws(new ObjectNotFoundException(pageId, "CmsPage"));
            NotFound404Result result = this.c.Content(pageId, "any string") as NotFound404Result;
            Assert.IsNotNull(result);
        }

         [Test]
        public void Should_return_Links_collection()
        {
             
   /*          _dataContext.Object.ac_Pages.AddObject(new ac_Pages(){Id = 1, EntryTitle = "hello1"});
             _dataContext.Object.ac_Pages.AddObject(new ac_Pages(){Id = 2, EntryTitle = "hello2"});
             _dataContext.Object.ac_Pages.AddObject(new ac_Pages(){Id = 3, EntryTitle = "hello3"});
             _dataContext.Object.ac_Tags.AddObject(new ac_Tags(){TagId = 1, TagName = "hello"});
             _dataContext.Object.ac_Tags.AddObject(new ac_Tags(){TagId = 2, TagName = "world"});
             _dataContext.Object.ac_Tags.AddObject(new ac_Tags(){TagId = 3, TagName = "Yes"});
             _dataContext.Object.ac_PageTags.AddObject(new ac_PageTags(){PageId = 1, TagId = 1});
             _dataContext.Object.ac_PageTags.AddObject(new ac_PageTags(){PageId = 1, TagId = 2});
             _dataContext.Object.ac_PageTags.AddObject(new ac_PageTags(){PageId = 1, TagId = 3});
             _dataContext.Object.ac_PageTags.AddObject(new ac_PageTags(){PageId = 2, TagId = 2});
             _dataContext.Object.ac_PageTags.AddObject(new ac_PageTags(){PageId = 3, TagId = 1});
             _dataContext.Object.ac_PageTags.AddObject(new ac_PageTags(){PageId = 3, TagId = 2});
            string tag = "admin";
             ViewResult vr = c.Tag(tag);
             Assert.IsNotNull(vr);
             List<Page> pages = vr.ViewData.Model as List<Page>;
             Assert.IsNotNull(pages);
             Assert.IsNotEmpty(pages);*/
        }
    }
}